﻿using System;
using System.Windows;
using System.Windows.Controls;

using Microsoft.Xna.Framework.Content;

using System.Windows.Resources;
using Microsoft.Xna.Framework.Graphics;

namespace Microsoft.Xna.Framework.Media
{
	/// <summary>
	/// Provides access to a song in the song library.
	/// </summary>
    public sealed class Song : IEquatable<Song>, IDisposable
    {
        internal MediaElement songElement;
        private string _path;
        private GraphicsDevice _graphics;
		private int _hashcode;
        private bool _repeating = false;
        private StreamResourceInfo resourceInfo;

		#region Properties
		/// <summary>
		/// Gets the name of the Song.
		/// </summary>
		public string Name
		{
			get { return songElement.Name; }
		}
		#endregion

		internal Song(ContentManager content, string assetName)
        {
            _path = assetName;
            resourceInfo = Application.GetResourceStream(new Uri(_path + ".mp3", UriKind.Relative));
            if (resourceInfo == null)
            {
                resourceInfo = Application.GetResourceStream(new Uri(_path + ".wma", UriKind.Relative));
            }

            _graphics = ((IGraphicsDeviceService)content.ServiceProvider.GetService(typeof(IGraphicsDeviceService))).GraphicsDevice;
        }

		public void Dispose()
		{
		}

		public override bool Equals(object obj)
		{
			Song song = obj as Song;

			return song != null ? this == song : false;
		}

		public bool Equals(Song other)
		{
			return this == other;
		}

		public static Song FromUri(string name, Uri uri)
		{
			throw new NotImplementedException();
		}

		public override int GetHashCode()
		{
			if (this._hashcode == -1)
			{
				this._hashcode = this.Name.GetHashCode();
			}
			return this._hashcode;
		}

		public override string ToString()
		{
			return this.Name;
		}

        void song_MediaEnded(object sender, RoutedEventArgs e)
        {
            if (_repeating)
            {
                songElement.Stop();
                songElement.Position = TimeSpan.Zero;
                songElement.Play();
            }
        }

        internal void SetRepeating(bool repeating)
        {
            _repeating = repeating;
        }

        internal void Play()
        {
            //Creating and setting source within constructor starts
            //playing song immediately.
            songElement = new MediaElement();
            songElement.MediaEnded += new RoutedEventHandler(song_MediaEnded);
            _graphics.Root.Children.Add(songElement);
            songElement.SetSource(resourceInfo.Stream);
            songElement.Volume = MediaPlayer.Volume;
            songElement.Play();
        }

        internal void SetVolume(float volume)
        {
            if (songElement != null)
                songElement.Volume = volume;
        }

        internal void Stop()
        {
            if (songElement != null)
            {
                _graphics.Root.Children.Remove(songElement);
                songElement.Stop();
            }
        }

		/// <summary>
		/// Gets the duration of the Song.
		/// </summary>
        public TimeSpan Duration
        {
            get { throw new NotImplementedException(); }
        }
        public bool IsRated
        {
            get { throw new NotImplementedException(); }
        }
        public int Rating
        {
            get { throw new NotImplementedException(); }
        }
        public int PlayCount
        {
            get { throw new NotImplementedException(); }
        }
        public int TrackNumber
        {
            get { throw new NotImplementedException(); }
        }

        internal void Resume()
        {
            songElement.Play();
        }

        internal void Pause()
        {
            songElement.Pause();
        }
    }
}
